﻿using System.Xml.Serialization;
using System.ComponentModel;
using System;
using System.Collections.Generic;

namespace OpenWings.Entities
{
    [Serializable]
    [XmlRoot("clients", Namespace = "", IsNullable = false)]
    public class ClientConfig : Entity
    {
        EntityList<Client> _Clients;

        public EntityList<Client> Clients
        {
            get
            {
                if (_Clients == null)
                {
                    _Clients = new EntityList<Client>();
                    _Clients.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(Clients_DirtyChanged);
                }

                return _Clients;
            }
        }

        void Clients_DirtyChanged(object sender, DirtyChangedEventArgs e)
        {
            IsDirty = e.Dirty;
        }

        [XmlElement("client")]
        public Client[] szClients
        {
            get
            {
                return Clients.ToArray();
            }
            set
            {
                if (value != null)
                {
                    _Clients = new EntityList<Client>((IEnumerable<Client>)value);
                    _Clients.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(Clients_DirtyChanged);
                }
            }
        }

        public override void OnDirtyTrackingChanged(object sender, DirtyTrackingChangedEventArgs e)
        {
            Clients.DirtyTracking = e.Enabled;
            base.OnDirtyTrackingChanged(sender, e);
        }

        public override void Clean()
        {
            _Clients.Clean();

            base.Clean();
        }
    }

    [Serializable]
    public class Client : Entity
    {
        string _Name;
        string _PathEXE;
        string _Arguments;
        string _DownloadURL;

        [XmlAttribute("name")]
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("pathexe")]
        public string PathEXE
        {
            get
            {
                return _PathEXE;
            }
            set
            {
                _PathEXE = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("arguments")]
        public string Arguments
        {
            get
            {
                return _Arguments;
            }
            set
            {
                _Arguments = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("downloadurl")]
        public string DownloadURL
        {
            get
            {
                return _DownloadURL;
            }
            set
            {
                _DownloadURL = value;
                IsDirty = true;
            }
        }
    }
}
